package com.example.qixinshangwutong.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.qixinshangwutong.R;


import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import cn.leancloud.LCUser;

import cn.leancloud.types.LCNull;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CompanyActivity extends AppCompatActivity {
    String url;
    String pinpai;
    String murl;
    LCUser currentUser;
    boolean type;
    String id;
    ImageView followCompany;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_company);
        initView();
        returnView();
        followCompany();

    }
    private void initView(){
        TextView companyName=findViewById(R.id.company_name_top);
        followCompany=findViewById(R.id.company_follow);
        WebView companyView=findViewById(R.id.company_webView);
        Intent intent=getIntent();
        if (intent.getExtras()!=null){
            url=intent.getExtras().getString("url");
            pinpai=intent.getExtras().getString("name");
            murl=intent.getExtras().getString("murl");
            companyName.setText(pinpai);
            companyView.loadUrl(url);
            currentUser=LCUser.getCurrentUser();
            if (currentUser!=null){
                LCQuery<LCObject> userQuery=new LCQuery<>("focus");
                userQuery.whereEqualTo("id",currentUser.getObjectId());
                userQuery.findInBackground()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<LCObject>>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull List<LCObject> lcObjects) {
                                for (int i = 0; i < lcObjects.size(); i++) {
                                    if (pinpai.equals(lcObjects.get(i).get("tittle"))){
                                        followCompany.setImageResource(R.drawable.gz_sel);
                                        type=true;
                                        id=lcObjects.get(i).getObjectId();


                                    }

                                }

                            }

                            @Override
                            public void onError(@NotNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }

        }

    }

    private void returnView(){
        ImageView back=findViewById(R.id.company_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CompanyActivity.this.finish();
            }
        });

    }

    private void followCompany(){
        followCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUser!=null){
                    if(type){
                        LCObject cancel=LCObject.createWithoutData("focus",id);
                        cancel.deleteInBackground().subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<LCNull>() {
                                    @Override
                                    public void onSubscribe(@NotNull Disposable d) {
                                        Toast.makeText(CompanyActivity.this,"已取消关注",Toast.LENGTH_SHORT).show();
                                        followCompany.setImageResource(R.drawable.gz);
                                        type=false;
                                    }

                                    @Override
                                    public void onNext(@NotNull LCNull lcNull) {

                                    }

                                    @Override
                                    public void onError(@NotNull Throwable e) {
                                        Toast.makeText(CompanyActivity.this,"取关失败",Toast.LENGTH_SHORT).show();

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });
                    }else{
                        LCObject follow=new LCObject("focus");
                        follow.put("tittle",pinpai);
                        follow.put("url",url);
                        follow.put("id",currentUser.getObjectId());
                        follow.put("imageurl",murl);
                        follow.saveInBackground().subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<LCObject>() {
                                    @Override
                                    public void onSubscribe(@NotNull Disposable d) {

                                    }

                                    @Override
                                    public void onNext(@NotNull LCObject lcObject) {
                                        followCompany.setImageResource(R.drawable.gz_sel);
                                        type=true;
                                        Toast.makeText(CompanyActivity.this,"关注成功",Toast.LENGTH_SHORT).show();

                                    }

                                    @Override
                                    public void onError(@NotNull Throwable e) {
                                        Toast.makeText(CompanyActivity.this,"关注失败",Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });
                    }
                }else{
                    Intent intent=new Intent(CompanyActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
            }
        });


    }
}